package com.fgs.common.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.fgs.common.CommonFragment;
import com.fgs.common.utils.WrapContentHeightViewPager;
import com.fgs.common.widget.CustomViewPager;

import java.util.List;

/**
 * @author fgs
 * @date 2020/3/28
 **/
public class FragmentPagerAdapter extends androidx.fragment.app.FragmentPagerAdapter {
    private List<CommonFragment> fragments;
    private List<String> tabTitles;
    private final FragmentManager fm;

    public FragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this .fm = fm;
    }

    public void setFragments(List fragments) {
        this.fragments = fragments;
    }

    public void setTabTitles(List<String> tabTitles) {
        this.tabTitles = tabTitles;
    }

    @Override
    public Fragment getItem(int i) {
        return fragments == null ? null : fragments.get(i);
    }

    @Override
    public int getCount() {
        return fragments == null ? 0 : fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        if (null == tabTitles) {

            return super.getPageTitle(position);
        } else {
            return tabTitles.get(position);
        }
    }

//    public void refresh(List<CommonFragment> fragmentList){
//        if (this.fragments!=null) {
//            FragmentTransaction ft = fm.beginTransaction();
//            for (Fragment fragment : this.fragments) {
//                ft.remove(fragment);
//            }
//            ft.commit();
////            ft = null;
//            fm.executePendingTransactions();
//        }
//            this .fragments = fragmentList;
//            this .notifyDataSetChanged();
//    }


    /**
     *    动态刷新数据后，刷新界面
     **/
    public void RefreshFragments(CustomViewPager mViewPager, List<CommonFragment> newfragments, List<String> tabList) {

        this.tabTitles = tabList;
        if (!fragments.isEmpty()) {
            fragments.clear();
        }
        fragments.addAll(newfragments);
        notifyDataSetChanged();
        mViewPager.setOffscreenPageLimit(newfragments.size());

    }



    //将每一个item的id指定为fragment的hasCode，确保不会变更
    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Fragment instantiateItemFragment = (Fragment) super.instantiateItem(container, position);
        Fragment itemFragment = fragments.get(position);
        //如果集合中对应下标的fragment和fragmentManager中的对应下标的fragment对象一致，则直接返回该fragment
        if (instantiateItemFragment == itemFragment) {
            return instantiateItemFragment;
        } else {
            //如果集合中对应下标的fragment和fragmentManager中的对应下标的fragment对象不一致，那么就是新添加的，所以自己add进入；
            fm.beginTransaction().add(container.getId(), itemFragment).commit();
            return itemFragment;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        Fragment fragment = (Fragment) object;
        //如果getItemPosition中的值为PagerAdapter.POSITION_NONE，就执行该方法。
        if (fragments.contains(fragment)) {
            super.destroyItem(container, position, object);
            return;
        }
        //自己执行移除。因为mFragments在删除的时候就把某个fragment对象移除了，所以一般都得自己移除在fragmentManager中的该对象。
        if (!fm.isStateSaved()) {
            fm.beginTransaction().remove(fragment).commit();
        }
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        //如果fragment还没有添加过，或者没有包含在里面，则返回没有找到
        if (!((Fragment) object).isAdded() || !fragments.contains(object)) {
            return POSITION_NONE;
        }
        //否则就返回列表中的位置
        return fragments.indexOf(object);
    }



}
